package cn.fl.capital.service.impl;

import cmm.user.facade.system.dto.SysOrgDTO;
import cn.fl.capital.condition.CapFundQueryCondition;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.vo.CapFundVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.capital.dto.CapFundIntoLogDTO;
import cn.fl.capital.condition.CapFundIntoLogQueryCondition;
import cn.fl.capital.api.ICapFundIntoLogApiService;
import cn.fl.capital.service.ICapFundIntoLogService;
import cn.fl.capital.transform.CapFundIntoLogVOTransform;
import cn.fl.capital.vo.CapFundIntoLogVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 资金池转入记录表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapFundIntoLogServiceImpl implements ICapFundIntoLogService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapFundIntoLogServiceImpl.class);
	
	/**
	 * 资金池转入记录表 暴露服务
	 */
	@Autowired
	private ICapFundIntoLogApiService capFundIntoLogApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public CapFundIntoLogVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapFundIntoLogDTO dto = this.capFundIntoLogApiService.selectCapFundIntoLogById(id);
		return CapFundIntoLogVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapFundIntoLogVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capFundIntoLogApiService.addCapFundIntoLog(CapFundIntoLogVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capFundIntoLogApiService.removeCapFundIntoLogByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapFundIntoLogVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capFundIntoLogApiService.modifyCapFundIntoLog(CapFundIntoLogVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapFundIntoLogQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapFundIntoLogQueryCondition());
        }
		condition=(CapFundIntoLogQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capFundIntoLogApiService.searchCapFundIntoLogListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapFundIntoLogVO> voList=CapFundIntoLogVOTransform.toVOList((List<CapFundIntoLogDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapFundIntoLogVO> searchList(CapFundIntoLogQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapFundIntoLogQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapFundIntoLogVO> voList=CapFundIntoLogVOTransform.toVOList(this.capFundIntoLogApiService.searchCapFundIntoLogList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapFundIntoLogVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		Map<String, String> fundStatusCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_CAP_FUND_STS_CD);

		for (CapFundIntoLogVO vo : voList) {
			if (fundStatusCdMap != null && fundStatusCdMap.containsKey(vo.getFundStatusCd())) {
				vo.setFundStatusCdNm(fundStatusCdMap.get(vo.getFundStatusCd()));
			}
		}
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CapFundIntoLogConstant.CODE_CD);
		for(CapFundIntoLogVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}
		}*/
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	@Override
	public Pager selectFundToBeUsedListPage(Pager page) throws BaseException {
		if (null == page) {
			page = new Pager();
			page.setCondition(new CapFundIntoLogQueryCondition());
		}
		page = this.capFundIntoLogApiService.selectFundToBeUsedListPage(page);
		if (CollectionUtils.isNotEmpty(page.getResultList())) {
			List<CapFundIntoLogVO> voList = CapFundIntoLogVOTransform.toVOList((List<CapFundIntoLogDTO>) page.getResultList());
			this.setDictData(voList);
			page.setResultList(voList);
		}
		return page;
	}

	@Override
	public List<CapFundIntoLogVO> selectFundsToApplyTransIncomeList(List<Long> ids) throws BaseException {
		List<CapFundIntoLogVO> voList = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(ids)) {
			List<CapFundIntoLogDTO> capFundIntoLogDTOS = capFundIntoLogApiService.selectFundByIntoIds(ids);

			if (CollectionUtils.isNotEmpty(capFundIntoLogDTOS)) {
				voList = CapFundIntoLogVOTransform.toVOList(capFundIntoLogDTOS);
			}
		}
		return voList;
	}

}
